{% extends 'base.html' %}

{% block main %}

<div class="container">
    <div class="row clearfix">
        <h2 class="text-center">菜品列表</h2>
        <button type="button" class="btn btn-success" data-toggle="modal" data-target="#myModal"><i
                class="glyphicon glyphicon-plus"></i> 增加</button>



        <div class="modal fade" id="myModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="myModalLabel">
                            增加菜品
                        </h4>
                    </div>
                    <div class="modal-body">
                        <form role="form" action="{% url 'BoosAPP:add_food'%}" method="post" enctype="multipart/form-data">
                            {% csrf_token %} 
                            <div class="form-group">
                                <label for="name">菜品名称</label><input type="text" class="form-control"
                                    id="name" name="name" />
                            </div>
                            <div class="form-group">
                                <label for="price">价格</label><input type="text"
                                    class="form-control" id="price" name="price"/>
                            </div>
                            <div class="form-group">
                                <label for="info">简介 </label><input type="text"
                                    class="form-control" id="info" name="info" />
                            </div>
                            
                            <div class="form-group">
                                <label for="f_type">菜品类型 </label>
                                <select class="form-control" id="f_type" name="f_type">
                                    {% for t in f_types %}
                                    <option value="{{t.id}}">{{t.name}}</option>
                                    {% endfor %}
                                    
                                  </select>
                            </div>
                            <div class="form-group">
                                <label for="img_file">菜品图片  </label><input type="file" class="form-control" id="img_file" name="img_file" />
                            </div>
                            
                            
                            
                           <button type="submit" class="btn btn-primary" >添加</button>
                        </form>


                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button> 
                        <!-- <button type="button" class="btn btn-primary">保存</button> -->
                    </div>
                </div>

            </div>

        </div>
        <script >
           const csrf= $("[name='csrfmiddlewaretoken']").val();

        </script>


        <div class="col-md-12 column">
            <div class="row clearfix">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>
                                编号
                            </th>
                            <th>
                                菜品名称
                            </th>
                            <th>
                                图片
                            </th>
                            <th>
                                简介
                            </th>
                            <th>
                                价格
                            </th>
                            <th>
                                操作
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for food in page_foods %}
                        <tr>
                            <td>
                                {{food.id}}
                            </td>
                            <td>
                                {{food.name}}
                            </td>
                            <td>
                                <img src="{{food.img}}" alt="" width="100px" height="100px">
                            </td>
                            <td>
                                {{food.info}}
                            </td>
                            <td>
                                {{food.price}}
                            </td>
                            <td>
                                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#food{{food.id}}"><i class="glyphicon glyphicon-pencil"></i>
                                    修改</button>
                                <a type="button" href="{% url 'BoosAPP:delete_food'%}?fid={{food.id}}" class="btn btn-danger"><i class="glyphicon glyphicon-trash"></i>
                                    删除</a>
                            </td>
                            <!-- 修改模态框 -->
                            <div class="modal fade" id="food{{food.id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                            <h4 class="modal-title" id="myModalLabel">修改{{food.name}}</h4>
                                        </div>
                                        <div class="modal-body">
                                            <form role="form" action="{% url 'BoosAPP:update_food'%}" method="post" enctype="multipart/form-data">
                                                {% csrf_token %} 
                                                <input type="hidden" name="fid" id="fid" value="{{food.id}}">
                                                <div class="form-group">
                                                    <label for="name">菜品名称</label><input type="text" class="form-control"
                                                        id="name" name="name" value="{{food.name}}" />
                                                </div>
                                                <div class="form-group">
                                                    <label for="price">价格</label><input type="text"
                                                        class="form-control" id="price" name="price" value="{{food.price}}"/>
                                                </div>
                                                <div class="form-group">
                                                    <label for="info">简介 </label><input type="text"
                                                        class="form-control" id="info" name="info" value="{{food.info}}" />
                                                </div>
                                                
                                                <div class="form-group">
                                                    <label for="f_type">菜品类型 </label>
                                                    <select class="form-control" id="f_type" name="f_type">
                                                        {% for t in f_types %}
                                                        {% if t.id == food.f_type.id %}
                                                        <option value="{{t.id}}" selected>{{t.name}}</option>
                                                        {% else %}
                                                        <option value="{{t.id}}">{{t.name}}</option>
                                                        {% endif %}
                                                        {% endfor %}
                                                        
                                                      </select>
                                                </div>
                                                <div class="form-group">
                                                    <img src="{{food.img}}" alt="" width="200px" height="200px">
                                                    <label for="img_file">菜品图片  </label><input type="file" class="form-control" id="img_file" name="img_file"  />
                                                </div>
                                                
                                                
                                                
                                               <button type="submit" class="btn btn-primary" >修改</button>
                                            </form>
                                            

                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                            <!-- <button type="button" class="btn btn-primary">提交更改</button> -->
                                        </div>
                                    </div><!-- /.modal-content -->
                                </div><!-- /.modal -->
                            </div>
                            <!-- 修改模态框 end -->
                        </tr>
                        {% endfor %}

                    </tbody>
                </table>



            </div>
        </div>

        <div class="col-md-12 column text-center">
            <ul class="pagination ">
                {% if  page_foods.has_previous %}
                <li>
                    <a href="{% url 'BoosAPP:edit_food' %}?page={{page_foods.previous_page_number }}">前一页</a>
                </li>
                {% else %}
                <li class="disabled">
                    <a href="">前一页</a>
                </li>

                {% endif %}
                {% for p in paginator.page_range  %}

                <li {% if p == page %} class="active" {% endif %}>
                    <a href="{% url 'BoosAPP:edit_food' %}?page={{p}}">{{p}}</a>
                </li>
                {% endfor %}

                {% if  page_foods.has_next %}
                <li>
                    <a href="{% url 'BoosAPP:edit_food' %}?page={{page_foods.next_page_number  }}">后一页</a>
                </li>
                {% else %}
                <li class="disabled">
                    <span href="">后一页</span>
                </li>

                {% endif %}
            </ul>
        </div>
    </div>
</div>


{% endblock %}